package bm.com.project.server.project.service.impl;


import bm.com.framework.common.exception.BmException;
import bm.com.framework.security.util.SecurityUtils;
import bm.com.project.server.common.dto.SearchBaseDTO;
import bm.com.project.server.project.dto.BmAddMemberDTO;
import bm.com.project.server.project.entity.BmProjectMemberEntity;
import bm.com.project.server.project.mapper.BmProjectMemberMapper;
import bm.com.project.server.project.service.BmProjectMemberService;
import bm.com.framework.common.resp.PageVO;
import bm.com.framework.common.resp.ResultVO;
import bm.com.framework.common.unit.DateUnit;
import bm.com.framework.common.unit.Tool;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2024-05-31 16:33:33
 */
@Service("bmProjectMemberService")
public class BmProjectMemberServiceImpl extends ServiceImpl<BmProjectMemberMapper, BmProjectMemberEntity> implements BmProjectMemberService {

    @Resource
    BmProjectMemberMapper bmProjectMemberDao;

    /**
     * 添加成员
     * @param bmAddMemberDTO
     * @return
     */
    @Override
    public ResultVO add(BmAddMemberDTO bmAddMemberDTO) {
        try {

            QueryWrapper<BmProjectMemberEntity> objectQueryWrapper = new QueryWrapper<>();
            objectQueryWrapper.in("userId", bmAddMemberDTO.getUserIds());
            objectQueryWrapper.eq("projectId", bmAddMemberDTO.getProjectId());

            Long l = bmProjectMemberDao.selectCount(objectQueryWrapper);
            if (l > 0) {
                return ResultVO.error("该成员已在项目成员中");
            }
            List<BmProjectMemberEntity> list =  new ArrayList<>();

            /** 组装数据 */
            for (String one : bmAddMemberDTO.getUserIds()){
                BmProjectMemberEntity member = new BmProjectMemberEntity();
                member.setId(Tool.iDGenerator());
                member.setProjectId(bmAddMemberDTO.getProjectId());
                member.setUserId(one);
                member.setOrgId(SecurityUtils.getOrgId());
                member.setCreateDate(DateUnit.getNowTimeSecond());

                list.add(member);
            }

            bmProjectMemberDao.saveBatch(list);
        } catch (BmException be){
            return ResultVO.error("添加失败");
        }
        return ResultVO.success("添加成功");
    }

    /**
     * 获取列表
     * @param queue
     * @return
     */
    @Override
    public ResultVO getList(SearchBaseDTO queue) {

        if (queue.getPageNum() <= 1) {
            queue.setPageNum(1);
        }

        int count = bmProjectMemberDao.selectList(queue).size();
        queue.setPageOffset((queue.getPage() - 1) * queue.getPageNum());
        queue.setOrgId(SecurityUtils.getOrgId());
        List<BmProjectMemberEntity> memberList = bmProjectMemberDao.selectList(queue);
        PageVO<List> PageVO = new PageVO<>(count, memberList);
        return  ResultVO.success(PageVO);

    }

    /**
     * 删除信息
     * @param memberId
     * @return
     */
    @Override
    public ResultVO delete(long memberId) {

        QueryWrapper<Object> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("id", memberId);

        BmProjectMemberEntity bmProjectMemberEntity = bmProjectMemberDao.selectOne(objectQueryWrapper);

        if (Tool.isObjectEmpty(bmProjectMemberEntity)) {
            return ResultVO.error("该成员不在项目中");
        }

        bmProjectMemberDao.deleteById(memberId);
        return ResultVO.success("操作成功");
    }
}
